home *** CD-ROM | disk | FTP | other *** search
- Path: ibm32.perftech.com!usenet
- From: murf@perftech.com (John Murphy)
- Newsgroups: comp.lang.c
- Subject: Re: What is byte-alignment?
- Date: 8 Apr 1996 12:34:40 GMT
- Organization: Performance Technology Inc
- Message-ID: <4kb150$6eu@ibm32.perftech.com>
- References: <4jprfe$c6q@alcor.usc.edu>
- NNTP-Posting-Host: k5zba.perftech.com
- Mime-Version: 1.0
- NNTP-Posting-User: REVCO
- X-Newsreader: WinVN 0.93.11
-
- In article <4jprfe$c6q@alcor.usc.edu>, wawda@alcor.usc.edu says...
- >
- >I often hear the phrase "byte-aligment" in this newsgroups, yet have
- >no clue what it is. Can someone please explain it to me? It comes up
- >often when people talk about structures. Thanks in advance,
- >
- >A. Wawda
- >wawda@scf.usc.edu
- >
- Byte-alignment has to do with the address of an object relative to its
- size; bytes are always aligned, words are aligned only if they are
- located at even addresses, and double words are aligned only if they are
- located at addresses that are an even multiple of four.
-
- The importance of byte-alignment is that byte-aligned objects can be
- accessed with a single memory reference. Consider the simple case of a
- 16-bit machine with a 16-bit memory system (in which case we don't have
- to worry about double words, since they're not a fundamental data type
- of the machine): each memory read operation returns 16 bits (two bytes)
- of data. If you read a single byte at an even address, the neighboring
- byte at the next higher (odd) address is also read, but discarded; if
- you read a single byte at an odd address, the neighboring byte at the
- next lower (even) address is also read, but discarded. If you read a
- word at an even address, both bytes are read at once; but if you try to
- read a word at an odd address, things get more complicated! To read a
- word from address three, for instance, would require reading the pair of
- bytes at addresses two and three; discarding the bytes at address two
- and saving the byte at address three, shifted eight bits, for later;
- reading the pair of bytes at addresses four and five; discarding the
- byte at address five; and combining the byte at address four, shifted
- eight bits, with the saved and shifted byte from address three! Now,
- depending on your machine, that's either not quite as bad as it sounds,
- or much worse than it sounds. On some machines, including Intel based
- machines, the hardware takes care of all the details and the only
- penalty of reading a word on an odd boundary is the extra time required
- for the extra memory access; on other machines, however, the hardware
- simply refuses to deal with such a "complicated" operation, and your
- program is terminated with a nasty message about your attempt to access
- unaligned data.
-
- C compilers normally hide all the ugliness of byte-alignment from us by
- padding variables, structures, and malloc'ed data blocks so that
- everything is 'properly' aligned; this insures maximum efficiency (in
- some sense, at least) as well as insuring operation on the brain damaged
- machines that don't support non-aligned access. But there are a few
- cases where you need to be very aware of byte-alignment issues. One
- such case is when you build structures on the fly in malloc'ed or shared
- memory, in which case you may want (or need) to provide your on padding
- to keep things aligned. Another case is when you're dealing with
- externally defined data such as communication headers of disk data
- structures; if you don't disable your compiler's alignment padding, your
- structure may not match the actual data!
-
- Murf
-
-